package com.readboottest.controller;

import com.alibaba.fastjson.JSONObject;
import com.readboottest.entity.User;
import com.readboottest.entity.request.user.UserLoginRequest;
import com.readboottest.entity.response.BaseResponse;
import com.readboottest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.readboottest.constant.GlobalConstants.*;
import static com.readboottest.constant.UserConstants.*;

/**
 * @author zhanghuiyong
 * @创建时间 2019年07月16日
 * @描述: 用户模块
 **/

@RequestMapping("/rly/user/")
@RestController
@CrossOrigin
public class UserRestController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/regist")
    @ResponseBody
    public BaseResponse regist(@RequestBody @Valid User user) {
        BaseResponse registBaseResponse = new BaseResponse<>();
        //校验用户名是否存在
        try {
            System.out.println("开始用户注册:" + JSONObject.toJSONString(user));
            boolean isHaveUser = userService.queryIsHaveUser(user.getUsername());
            if (isHaveUser) {
                registBaseResponse.setMessage(USER_IS_HAVE);
                return registBaseResponse;
            }
            userService.registUser(user);
            registBaseResponse.setStatus(SUCCESS);
            registBaseResponse.setMessage(REGIST_SUCCESS);
            //去除密码  暂时
            user.setPassword(null);
            registBaseResponse.setResult(user);
            //session操作 暂时忽略
        } catch (Exception e) {
            e.printStackTrace();
            registBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束用户注册:" + JSONObject.toJSONString(registBaseResponse));
        return registBaseResponse;
    }

    /**
     * 用户登录功能
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public BaseResponse login(@RequestBody @Valid UserLoginRequest loginRequest) {
        BaseResponse loginBaseResponse = new BaseResponse<>();
        System.out.println("开始用户登录:" + JSONObject.toJSONString(loginRequest));
        try {
            List<User> users = userService.queryUserByAction(loginRequest);
            if (null == users || 1 > users.size()) {
                loginBaseResponse.setMessage(LOGIN_FAIL);
                return loginBaseResponse;
            }
            loginBaseResponse.setStatus(SUCCESS);
            loginBaseResponse.setResult(users.get(0));
        } catch (Exception e) {
            e.printStackTrace();
            loginBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束用户登录:" + JSONObject.toJSONString(loginBaseResponse));
        return loginBaseResponse;
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public BaseResponse updateUserInfo(@RequestBody User user) {
        BaseResponse loginBaseResponse = new BaseResponse<>();
        System.out.println("开始更新用户信息:" + JSONObject.toJSONString(user));
        try {
            //用户id不可为空
            if (null == user.getId()) {
                loginBaseResponse.setMessage(USER_ID_IS_NOT_NULL);
                return loginBaseResponse;
            }
            userService.updateUserInfoById(user);
            User newUser = userService.selectUserById(user.getId());
            loginBaseResponse.setStatus(SUCCESS);
            //去除密码
            newUser.setPassword(null);
            loginBaseResponse.setMessage(UPDATE_INFO_SUCCESS);
            loginBaseResponse.setResult(newUser);
        } catch (Exception e) {
            e.printStackTrace();
            loginBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束更新用户信息:" + JSONObject.toJSONString(loginBaseResponse));
        return loginBaseResponse;
    }


}
